package com.AnywheeBt.bean;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 类说明： 联系人实体类
 *
 * @author Everett Li
 * @version 1.0
 * @date 2014年6月19日
 */
public class ContactInfo {
    public static final String COMBINATION_SEPERATOR = ",";
    /**
     * 联系人号码列表
     */
    private final List<PhoneInfo> phoneInfos = new ArrayList<PhoneInfo>();
    /**
     * 联系人ID
     */
    private String contactId;
    /**
     * 联系人归一化名
     */
    private String[] nameNorm;
    /**
     * 联系人照片缩略图uri
     */
    private String photoThumbUri;

    /**
     * 联系人有多个选择时，选择的标准，比如，运营商、手机座机、归属地等，由内核给出
     */
    private String tag;

    /**
     * 名字
     */
    private String name;

    /**
     * 名字全拼
     */
    private String[] namePinyin;

    public String[] getNameNorm() {
        return nameNorm;
    }

    public void setNameNorm(String[] nameNorm) {
        this.nameNorm = nameNorm;
    }

    public List<PhoneInfo> getPhoneInfos() {
        return phoneInfos;
    }

    public String getContactId() {
        return contactId;
    }

    public void setContactId(String contactId) {
        this.contactId = contactId;
    }

    public String getPhotoThumbUri() {
        return photoThumbUri;
    }

    public void setPhotoThumbUri(String photoThumbUri) {
        this.photoThumbUri = photoThumbUri;
    }

    public void addPhoneInfo(PhoneInfo phoneInfo) {
        this.phoneInfos.add(phoneInfo);
    }

    public PhoneInfo getTopPhoneInfo() {
        if (phoneInfos != null && phoneInfos.size() > 0) {
            return phoneInfos.get(0);
        }
        return null;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    @Override
    public String toString() {
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < phoneInfos.size(); i++) {
//            sb.append("[");
//            sb.append(phoneInfos.get(i).toString());
//            sb.append("]\t");
//        }
//
//        return "{name: " + super.toString() + " nameNorm: " + nameNorm
//                + " pinyin: " + getPinyin() + " photoThumbUri: " + photoThumbUri + " phoneInfo: "
//                + sb.toString() + " }";
        return toJson2().toString();
    }

    /**
     * 返回ContactInfo的json对象
     *
     * @return json对象
     * @throws JSONException
     */
    public JSONObject toJson() {
        try {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("name", getName());
            JSONArray pinyinArray = new JSONArray();
            String pinyins[] = getPinyin();
            if (pinyins != null) {
                for (String pinyin : pinyins) {
                    pinyinArray.put(pinyin);
                }
            }
            jsonObj.put("pinyin", pinyinArray);
            JSONArray phoneInfoArray = new JSONArray();
            for (PhoneInfo phoneInfo : phoneInfos) {
                phoneInfoArray.put(phoneInfo.toJson());
            }
            jsonObj.put("phone_info", phoneInfoArray);
            JSONArray nameNormArray = new JSONArray();
            if (nameNorm != null) {
                for (String name : nameNorm) {
                    nameNormArray.put(name);
                }
            }
            jsonObj.put("name_norm", nameNormArray);
            return jsonObj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 返回ContactInfo的json对象
     *
     * @return json对象
     * @throws JSONException
     */
    public JSONObject toJson2() {
        try {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("name", getName());
//            JSONArray pinyinArray = new JSONArray();
//            String pinyins[] = getPinyin();
//            if (pinyins != null) {
//                for (String pinyin : pinyins) {
//                    pinyinArray.put(pinyin);
//                }
//            }
//            jsonObj.put("pinyin", pinyinArray);
            JSONArray phoneInfoArray = new JSONArray();
            for (PhoneInfo phoneInfo : phoneInfos) {
                phoneInfoArray.put(phoneInfo.toJson());
            }
            jsonObj.put("phone_info", phoneInfoArray);
//            JSONArray nameNormArray = new JSONArray();
//            if (nameNorm != null) {
//                for (String name : nameNorm) {
//                    nameNormArray.put(name);
//                }
//            }
//            jsonObj.put("name_norm", nameNormArray);
            return jsonObj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getPinyin() {
        return namePinyin;
    }

    public void setPinyin(String[] fullPinyin) {
        this.namePinyin = fullPinyin;
    }

    /**
     * 类说明： 电话信息类
     *
     * @author Everett Li
     * @version 1.0
     * @date 2014年6月19日
     */
    public static class PhoneInfo {

        /**
         * 号码正则：滤除+86的头，滤除-()和空格符号
         */
        public static final String PHONE_REGEX = "(^(\\+86)|[\\(\\)-[\\s]])";
        final static Map<String, String> OPERATOR_MAP = new HashMap<String, String>() {
            {
                put("130", "联通");
                put("131", "联通");
                put("132", "联通");
                put("133", "电信");
                put("134", "电信");
                put("134", "移动");
                put("135", "移动");
                put("136", "移动");
                put("137", "移动");
                put("138", "移动");
                put("139", "移动");
                put("145", "联通");
                put("147", "移动");
                put("150", "移动");
                put("151", "移动");
                put("152", "移动");
                put("153", "电信");
                put("155", "联通");
                put("156", "联通");
                put("157", "移动");
                put("158", "移动");
                put("159", "移动");
                put("180", "电信");
                put("181", "电信");
                put("182", "移动");
                put("183", "移动");
                put("185", "联通");
                put("186", "联通");
                put("187", "移动");
                put("188", "移动");
                put("189", "电信");
            }
        };
        /**
         * 手机号码正则
         */
        private final String MOBILE_NUM_REGEX = "[1]{1}[3,4,5,7,8]{1}[0-9]{9}";
        /**
         * 固定电话号码正则
         */
        private final String LANDLINE_NUM_REGEX = "[0]{0,1}[0-9]{2,3}[0-9]{7,8}";
        private Pattern mMobileNumPattern = Pattern.compile(MOBILE_NUM_REGEX);
        private Pattern mLandlineNumPattern = Pattern.compile(LANDLINE_NUM_REGEX);
        /**
         * 电话号码
         */
        private String number = "";
        /**
         * 电话Id
         */
        private String numId = "";
        /**
         * 归属地
         */
        private String location = "";
        /**
         * 运营商
         */
        private String operator = "";
        /**
         * 标记，如手机/座机
         */
        private String type = "";
        /**
         * 类型，如家庭/公司
         */
        private String flag = "";

        public PhoneInfo() {
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            if (!TextUtils.isEmpty(number)) {
                number = number.replaceAll(PHONE_REGEX, "");
                this.number = number;
            }
        }

        public String getNumId() {
            return numId;
        }

        public void setNumId(String numId) {
            this.numId = numId;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String callerCity) {
            this.location = callerCity;
        }

        public String getOperator() {
            return operator;
        }

        /**
         * 设置运营商
         */
        public void setOperator(String operator) {
            this.operator = operator;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }

        /**
         * 设置归属地和运营商
         *
         * @param area 包含有运营商和归属地信息的串，如"北京[移动]"
         */
        public void setLocationAndOperator(String area) {
            if (!TextUtils.isEmpty(area)) {
                location = area;
            }

            operator = OPERATOR_MAP.get(this.number.substring(0, 3));
        }

        /**
         * 根据数据库中存储的类型，确定号码的type和flag信息
         *
         * @param typeId 数据库中存储的类型枚举值
         * @param number 号码
         */
        public void setTypeAndTagByTypeId(int typeId, String number) {
            if (mMobileNumPattern.matcher(number).find()) {
                type = "手机";
            } else if (mLandlineNumPattern.matcher(number).find()) {
                type = "座机";
            }
        }

        @Override
        public String toString() {
            return "number: " + number + " callerloc: " + location + " operator: " + operator
                    + " type: " + type + " flag: " + flag;

        }

        /**
         * 返回PhoneInfo的json对象
         *
         * @return json对象
         * @throws JSONException
         */
        public JSONObject toJson() throws JSONException {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("number", number);
            jsonObj.put("location", location);
            jsonObj.put("operator", operator);
            jsonObj.put("type", type);
            jsonObj.put("flag", flag);
            return jsonObj;
        }
    }

}
